package com.zxl.thread.pool.callable;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class CallableDemo {

	public static void main(String[] args) {
		ExecutorService executorService = Executors.newCachedThreadPool();
		List<Future<String>> resultList=new ArrayList<>();
		
		for (int i = 0; i < 10; i++) {
			//使用ExecutorService执行Callable类型的任务，并将结果保存在future变量中
			Future<String> future = executorService.submit(new TaskWithResult(i));
			//将任务执行结果存储到List中 
			resultList.add(future);
		}
		
		for (Future<String> future : resultList) {
			try {
				System.out.println(future.get());//打印各个线程（任务）执行的结果 
			} catch (InterruptedException | ExecutionException e) {
				e.printStackTrace();
			}finally {
				//启动一次顺序关闭，执行以前提交的任务，但不接受新任务。如果已经关闭，则调用没有其他作用。
				executorService.shutdown();
			}
		}
	}
}
